home *** CD-ROM | disk | FTP | other *** search
- \
- \ STRUCTURE.ATL
- \
- \ Copyright (C) 1993 by Derrick Oswald
- \
- \ Derrick Oswald
- \ Nexsys Consulting Inc.
- \ 44 Douglas Drive
- \ Ayr, Ontario
- \ N0B 1E0
- \ (519) 632-8243
- \ (519) 632-8244 FAX
-
- \ Description:
- \ Allows aggregates of data to be described as structures. General-
- \ ization of structures in traditional programming languages. Allows
- \ definition, initialization and action part. Basic object based
- \ action may be defined in a style similar to the "does" section of
- \ a creating word.
- \
-
- .( "\nLoading Structures definitions..."
-
- \ field - creates a field definition at compile time
- \ at run time adds the field offset to the structure address
- : field ( offset -> )
- constant
- does>
- @ + ;
-
- \ size of compiled item
- 4 constant cell
-
- 0 field ->StructureSize ( struct.type -- addr)
- cell field ->Initiate ( struct.type -- addr)
-
- : as ( -- struct.type)
- ' >body ( Quote next symbol and access body)
- \ this should be
- \ [compile] literal ( If compiling generate a literal)
- \ but atlas complains if compiler words are used outside a : definition
- state @ if
- compile (lit) ,
- then
- ; immediate
-
- : this ( -- addr)
- last >body ( Access the body of the last symbol)
- ;
-
- : initiate ( addr struct.type -- )
- ->Initiate @ ?dup ( Access initiate. code pointer)
- if >r else drop then ( If available perform initialization)
- ;
-
- \ make-struct - reserve memory for a structure and initialize
- : make-struct ( struct.type -- addr)
- here dup >r ( Save pointer to instance)
- over ->StructureSize @ allot ( Access size and allocate memory)
- swap initiate r> ( Perform initialization)
- ;
-
- \ ?compile - compile or execute next threaded word depending on state
- : ?compile ( -- )
- state @ if
- r> dup , 4 + >r
- then
- ;
-
- \ new-struct - create structure of following type
- : new-struct ( -- addr)
- [compile] as ( Take the next symbol, "as")
- ?compile make-struct ( And "make" an instance)
- ; immediate
-
- \ sizeof - return the size of a structure
- : sizeof ( -- num)
- ' >body ->StructureSize @ ( Access size of structure)
- \ this should be
- \ [compile] literal ( And make literal if compiling)
- \ but atlas complains if compiler words are used outside a : definition
- state @ if
- compile (lit) ,
- then
- ; immediate
-
- \ assign - set structure data
- : assign ( a b -- )
- [compile] sizeof ?compile cmove ( Access size and assign instance)
- ; immediate
-
- \ .( "\nnot-equal"
- \ : not-equal ( a b -- bool)
- \ [compile] sizeof ?compile -match ( Access size and match the blocks)
- \ ; immediate
-
- \ struct.type - lead in word for structure definition
- : struct.type ( -- struct.type offset0)
- create
- here 4 + \ add 4 because ATLAS moves the word when does> encountered
- 0 0 , 0 , ( Allocate initial struct information)
- does> ( struct.type -- )
- create make-struct drop ( Create a new instance)
- ;
-
- : bytes ( offset1 n -- offset2)
- over field +
- \ over dup ( Check for zero offset)
- \ if field + ( Create an access field of "n" bytes)
- \ else
- \ create , + immediate ( Create an efficient field)
- \ does> ( field -- )
- \ drop ( Does nothing at runtime )
- \ then
- ;
-
- : align ( offset1 -- offset2)
- dup 1 and + ( Align field offset to even address)
- ;
-
- : struct.field ( bytes -- )
- create , 0 , ( Create a predefined field type)
- does> ( struct.field -- )
- @ bytes ( At run-time create field names)
- ;
-
- : struct ( -- )
- [compile] sizeof bytes ( Create a structure sized field name)
- ;
-
- ( Initial set of field names)
- 1 struct.field byte ( -- )
- 2 struct.field word ( -- )
- 4 struct.field long ( -- )
- 4 struct.field ptr ( -- )
- 4 struct.field enum ( -- )
-
- : struct.init ( struct.type offset3 -- )
- align over ->StructureSize ! ( Assign size of structure type)
- here swap ->Initiate ] ( And pointer to initialization code)
- ;
-
- : struct.does ( -- )
- [compile] does> ( Do what does-does)
- ; immediate
-
- : struct.end ( [] or [struct.type offset3] -- )
- state @ ( Check compilation status)
- if ['] EXIT , [COMPILE] [ ( If compiling then end definition)
- else swap ->StructureSize ! then ( Else assign size of structure type)
- ; immediate
-
- .( "\nLoaded.\n"